package com.hanhanxiaochong.zhongxiangchuanmei.presenter.my;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.BankCardBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.RechargeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WXPayBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IRechargeView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/6/11.
 * Desc   :
 */

public class RechargePresenter extends BasePresenter<IRechargeView> {

    public RechargePresenter(IRechargeView iView) {
        super(iView);
    }

    public void getBankCards(String token){
        addSubscription(RetrofitSource.createApi(HttpPostService.class).getBankCards(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<BankCardBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<BankCardBean>> response) {
                        iView.setBankcard(response.getData());
                    }
                }));
    }

    public void recharge(String token, String money, boolean rechargeFlag){
        addSubscription(RetrofitSource.createApi(HttpPostService.class).recharge(token, money)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<RechargeBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<RechargeBean> response) {
                        RechargeBean data = response.getData();
                        String order_num = data.getOrder_num();
                        String money = data.getMoney();
                        if (rechargeFlag){
                            wxPay(order_num, money);
                        }else {
                            alipay(order_num, money);
                        }
                    }
                }));
    }

    private void alipay(String orderNum, String money) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .alipaynotify_url(orderNum, money)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.aliPay(response.getData());
                    }
                }));
    }

    private void wxPay(String orderNum, String money) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .WxPay(money, orderNum, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<WXPayBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<WXPayBean> response) {
                        iView.wxPay(response.getData());
                    }
                }));
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }
}
